﻿using NUnit.Framework;
using Sasa.QualityTools.CodeAnalysis.Core.Requests;
using Sasa.QualityTools.CodeAnalysis.Core.Services;
using Sasa.QualityTools.CodeAnalysis.Core.Services.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.Core.Tests.Integration.Services
{
    [TestFixture]
   public  class CodeAnalysisServiceTest
    {
        private static readonly string ResourceDirectory = Path.Combine("Resources", "Services", "CodeAnalysisService");

        private static readonly string FxCopCmdPath = Path.Combine(Environment.GetEnvironmentVariable("PROGRAMFILES"), "Microsoft FxCop 10.0", "FxCopCmd.exe");

        private CodeAnalysisService testee;


        [SetUp]
        public void SetUp()
        {
            testee = new CodeAnalysisService();
        }

        [Test]
        [Description("Execute should perform code analysis with the specified assembly.")]
        public void TestExecuteForAssembly()
        {
            var request = new AssemblyAnalysisRequest();
            request.FxCopCmdPath = FxCopCmdPath;
            request.AssemblyPath = Path.Combine(ResourceDirectory, "TestTarget.dll");
            request.EnableSearchGac = true;
            request.IgnoreGeneratedCode = true;
            request.Timeout = 180;
            request.RuleSetPath = Path.Combine(ResourceDirectory, "Custom.ruleset");

            var actual = testee.Execute(request);

            Assert.AreEqual(18, actual.Problems.Count);
            Assert.AreEqual(12, actual.Rules.Count);
        }

        [Test]
        [Description("Execute should perform code analysis with the specified FxCop project.")]
        public void TestExecuteForFxCopProject()
        {
            var request = new FxCopProjectAnalysisRequest();
            request.FxCopCmdPath = FxCopCmdPath;
            request.FxCopProjectPath = Path.Combine(ResourceDirectory, "Project.FxCop");

            var actual = testee.Execute(request);

            Assert.AreEqual(10, actual.Problems.Count);
            Assert.AreEqual(9, actual.Rules.Count);
        }
    }
}
